import Tab from '../tab/tab.vue'
import Pages from '../../../pages/pages.vue'
import { getCommissionList, updateCommissionList, showMsg, setMenuType } from '../../../../vuex/action'
import { modal } from 'vue-strap'
export default {
  name: 'commission-list',
  components: {
    'vue-modal': modal,
    'vue-tab': Tab,
    'vue-pages': Pages,
  },
  data() {
    return {
      showConfirm: false,
      currentId: '',
      currentType: '',
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      phone: '',
      status: '',
      checkedIds: [],
      checkAll: false,
      noSelectUser: true
    }
  },
  vuex: {
    getters: {
      fresh: state => state.dataflowListNeedFresh
    },
    actions: {
      xGetCommissionList: getCommissionList,
      xUpdateCommissionList: updateCommissionList,
      xShowMsg: showMsg,
      xSetMenuType: setMenuType
    }
  },
  ready() {
    this.xSetMenuType('activity')
    this.getList()
  },
  methods: {
    getList() {
      var param = {
        start: this.pages.start,
        limit: this.pages.limit
      }
      if (this.phone) {
        param.phone = this.phone
      }
      if (this.status) {
        param.status = this.status
      }
      this.xGetCommissionList(param)
        .then((data) => {
          this.list = data.data.list
          this.pages.totalPages = Math.ceil(data.totalCount / this.pages.limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    update(id, type, fundId) {
      this.currentId = id
      this.currentType = type
      if(+type ===2 && !fundId){
        this.showConfirm = true
      } else {
        this.submit()
      }
    },
    submit() {
      this.xUpdateCommissionList({
        ids: this.currentId,
        status: this.currentType
      })
        .then((data) => {
          this.showConfirm = false
          this.xShowMsg({
            type: 'info',
            content: data.error_msg
          })
          this.getList()
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    batch() {
      let ids =  this.checkedIds.join(',')
      let pass = true
      this.currentId = ids
      this.currentType = 2
      for(let i=0; i<this.checkedIds.length; i++){
        let fundId = document.getElementById(this.checkedIds[i]).getAttribute('data-fundId')
        if(!fundId){
          this.showConfirm = true
          pass = false
          break
        }
      }
      if(pass){
        this.submit()
      }
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    },
    checkAll() {
      this.checkedIds = []
      for (var item of this.list) {
        if (this.checkAll && +item.status===1) {
          this.checkedIds.push(item.id + '')
        }
      }
    },
    checkedIds() {
      if (this.checkedIds.length === 0) {
        this.noSelectUser = true
      } else {
        this.noSelectUser = false
      }
    }
  }
}
